#!/usr/bin/env python
from tempfile import mkstemp
from shutil import move
from os import remove, close
from os import system
def replace(file_path, pattern, subst, count):
    #Create temp file
    fh, abs_path = mkstemp()
    new_file = open(abs_path,'w')
    old_file = open(file_path)
    print(old_file)
    for line in old_file:
        new_file.write(line.replace(pattern, subst, count))
    #close temp file
    new_file.close()
    close(fh)
    old_file.close()
    #Remove original file
    remove(file_path)
    #Move new file
    move(abs_path, file_path)

#import fileinput
#import sys
#def replaceAll(file,searchExp,replaceExp,count):
#    for line in fileinput.input(file, inplace=1):
#        if searchExp in line:
#            line = line.replace(searchExp,replaceExp,count)
#        sys.stdout.write(line)

#Ant      *.build.xml
#Asm      *.asm *.ASM *.s *.S *.A51 *.29[kK] *.[68][68][kKsSxX] *.[xX][68][68]
#Asp      *.asp *.asa
#Awk      *.awk *.gawk *.mawk
#Basic    *.bas *.bi *.bb *.pb
#BETA     *.bet
#C        *.c
#C++      *.c++ *.cc *.cp *.cpp *.cxx *.h *.h++ *.hh *.hp *.hpp *.hxx *.C *.H
#C#       *.cs
#Cobol    *.cbl *.cob *.CBL *.COB
#DosBatch *.bat *.cmd
#Eiffel
#Erlang   *.erl *.ERL *.hrl *.HRL
#Flex     *.as *.mxml
#Fortran  *.f *.for *.ftn *.f77 *.f90 *.f95 *.F *.FOR *.FTN *.F77 *.F90 *.F95
#HTML     *.htm *.html
#Java     *.java
#JavaScript *.js
#Lisp     *.cl *.clisp *.el *.l *.lisp *.lsp
#Lua      *.lua
#Make     *.mak *.mk [Mm]akefile GNUmakefile
#MatLab   *.m
#OCaml    *.ml *.mli
#Pascal   *.p *.pas
#Perl     *.pl *.pm *.plx *.perl
#PHP      *.php *.php3 *.phtml
#Python   *.py *.pyx *.pxd *.pxi *.scons
#REXX     *.cmd *.rexx *.rx
#Ruby     *.rb *.ruby
#Scheme   *.SCM *.SM *.sch *.scheme *.scm *.sm
#Sh       *.sh *.SH *.bsh *.bash *.ksh *.zsh
#SLang    *.sl
#SML      *.sml *.sig
#SQL      *.sql
#Tcl      *.tcl *.tk *.wish *.itcl
#Tex      *.tex
#Vera     *.vr *.vri *.vrh
#Verilog  *.v
#VHDL     *.vhdl *.vhd
#Vim      *.vim
#YACC     *.y
#e        *.e

#nospace allowd for each type line, and no space after \ each line
#target vobs list
#options
#actions

import os
import sys
findcmdBig = """find \
  . \
  -regex '.*\.\(\
c\
\|c++\|cc\|cpp\|h\|hpp\|C\|H\
\|e\
\|mak\|mk\|[Mm]akefile\|cmake\
\|cs\
\|m\
\|pl\|pm\|perl\
\|py\
\|gel\
\|rb\|ruby\
\|sh\|SH\|csh\
\|jar\|java\
\|tcl\|tk\|itcl\|wish\
\|xml\
\|exe\|bat\
\|sig\
\|efx\|emx\
\|log\|txt\|lnt\|pdf\|doc\|xls\
\|spec\|rcw\|rls\con\|cfg\|cmd\|out\
\|errors\
\)'\
  -type f -printf '%f\\t%p\\t1\\n' > filetags """

findcmdSmall = """find \
  . \
  -regex '.*\.\(\
c\
\|c++\|cc\|cpp\|h\|hpp\|C\|H\
\|e\
\|cs\
\|pl\|pm\|perl\
\|py\
\|sig\
\)'\
  -type f -printf '%f\\t%p\\t1\\n' > filetags """

if sys.platform== 'win32':
    findcmdSmall = "find -L . -name '*.c' -o -name '*.h' -o -name '*.cpp' -o -name '*.hpp' -printf  '%f\\t%p\\t1\\n' > filetags"
    findcmdBig = "find -L . -name '*.c' -o -name '*.h' -o -name '*.cpp' -o -name '*.hpp' -printf  '%f\\t%p\\t1\\n' > filetags"

def fileTags(bigOrSmall):
  if bigOrSmall:
    findcmd = findcmdBig
  else:
    findcmd = findcmdSmall

  if os.path.exists("filetags"):
    print("clean exist tags, and regenerte new tags")
    os.remove("filetags")
    print(findcmd)
    os.system(findcmd)
    absPath = os.path.abspath(".")
    absPath = absPath + '/'
    replace("filetags","./",absPath,1)
    print("done ^_^")
  else:
    print("generte new filetags")
    print(findcmd)
    os.system(findcmd)
    absPath = os.path.abspath(".")
    absPath = absPath + '/'
    replace("filetags","./",absPath,1)
    print("done ^_^")


def printUsage():
    print (
    """Usage: myFiletags [bool]
         if true, means big filetags which support many different extensions will be generated,
         if false , small filetags file with general c++/ e, python, perl files will be generated
         and default is false
    """)

def main(argv):
  try:
    bigOrSmall = False
    if len(argv) > 1:
      bigOrSmall = argv[0]

    fileTags(bigOrSmall)
  except:
    printUsage()





if __name__ == "__main__":
  import sys
  #main(sys.argv[1:]) ## sends all but the scriptname as argument
  main(sys.argv)
